/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 04.03.12
 * Time: 22:21
 * To change this template use File | Settings | File Templates.
 */

function SimpleAI(id) {
    this.setID(id);
    this.following = null;
    this.path = [];
}
SimpleAI.inherits(Player);

SimpleAI.method('update', function () {
    this.uber('update'); // call the inherited Player update!
    var action = this.evaluateField();
    switch (action) {
        case AIStates.FOLLOW:
            if (this.following) {
                if (this.evaluateNearest()) {
                    this.getFollowingPath();
                }
                this.go();
            } else {
                this.following = this.findNearest();
                this.getFollowingPath();
                this.go();
            }
            break;
        default:
            break;
    }

});
SimpleAI.method('evaluateNearest', function () {
    for (var i = 0; i < OurGame.players.length; i++) {
        if (OurGame.players[i].id == this.following.id) {
            if (OurGame.players[i].x != this.following.x || OurGame.players[i].y != this.following.y) { //if is changed
                this.following.x = OurGame.players[i].x;
                this.following.y = OurGame.players[i].y;
                return true;
            }
            return false;
        }
    }
});
SimpleAI.method('go', function () {
    var nextStep = this.path[0];
    if (nextStep) {
        if (!this.isMoving()) {
            if (isPlaceFree(nextStep.x, nextStep.y)) {
                this.moveX(nextStep.x - (this.x / OurGame.grid));
                this.moveY(nextStep.y - (this.y / OurGame.grid));
                this.path.shift(); //remove done step
            }
        }

    }
});
SimpleAI.method('getFollowingPath', function () {
    if (!this.graph) {
        this.graph = lazyInit();
    }

    if (!this.isMoving()) { //if full step has been made
        var start = this.graph.nodes[(this.x / OurGame.grid)][(this.y / OurGame.grid)];
        var end = this.graph.nodes[Math.floor((this.following.x / OurGame.grid))][Math.floor((this.following.y / OurGame.grid))];
//            console.log(start);
//            console.log(end);
        this.path = astar.search(this.graph.nodes, start, end);
    }

});
SimpleAI.method('findNearest', function () {
    var minDistance = 19 * 16 * 32;
    var nearestPlayer = null;

    for (var i = 0; i < OurGame.players.length; i++) {
        var player = OurGame.players[i];
        if (player.id != this.id) {
            var currentDistance = computeManhattanDistance(this, player);
            if (currentDistance < minDistance) {
                minDistance = currentDistance;
                nearestPlayer = player;
            }
        }
    }

    return {id:nearestPlayer.id, distance:minDistance, time:0, x:nearestPlayer.x, y:nearestPlayer.y};
});
SimpleAI.method('evaluateField', function () {
    return AIStates.FOLLOW;
});

function computeManhattanDistance(src, target) {
    var d1 = Math.abs(src.x - target.x);
    var d2 = Math.abs(src.y - target.y);
    return d1 + d2;
}

function lazyInit() {
    return new Graph(OurGame.booleanMap);
}